import java.util.Arrays;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86185
 * Date: 2023-05-05
 * Time: 18:49
 */
public class Test {
    public static void merge(int[] arr, int left,int mid, int right) {
        int s1 = left;
        int e1 = mid;
        int s2 = mid + 1;
        int e2 = right;
        int k = 0;
        int[] tmpArr = new int[right - left + 1];
        while(s1 <= e1 && s2 <= e2) {
            if(arr[s1] < arr[s2]) {
                tmpArr[k++] = arr[s1++];
            } else {
                tmpArr[k++] = arr[s2++];
            }
        }
        while(s1 <= e1) {
            tmpArr[k++] = arr[s1++];
        }
        while(s2 <= e2) {
            tmpArr[k++] = arr[s2++];
        }
        for (int i = 0; i < k; i++) {
            arr[i + left] = tmpArr[i];
        }
    }
    public static void mergeFunc(int[] arr, int start, int end) {
        if(start >= end) {
            return;
        }
        int mid = (start + end) / 2;
        mergeFunc(arr, start, mid);
        mergeFunc(arr, mid + 1, end);
        merge(arr, start, mid, end);
    }
    public static void mergeSort(int[] arr) {
        mergeFunc(arr, 0, arr.length - 1);
    }
    public static void mergeSort2(int[] arr) {
        int gap = 1;
        while(gap < arr.length) {
            for (int i = 0; i < arr.length; i += gap * 2) {
                int left = i;
                int mid = left + gap - 1;
                int right = mid + gap;
                if(mid >= arr.length) {
                    mid = arr.length - 1;
                }
                if(right >= arr.length) {
                    right = arr.length - 1;
                }
                merge(arr, left, mid, right);
            }
            gap *= 2;
        }
    }

    public static void main(String[] args) {
        int[] arr = {6, 5, 9, 8, 3, 4, 7, 1, 2};
//        mergeSort(arr);
        mergeSort2(arr);
        System.out.println(Arrays.toString(arr));
    }
    public static void selectSort(int[] arr) {
        int index = 0;
        for (int i = 0; i < arr.length; i++) {
            for (int j = i + 1; j < arr.length; j++) {
                if(arr[i] < arr[index]) {
                    index = i;
                }
            }
            swap(arr, i ,index);
        }
    }
    private static void swap(int[] array,int i,int j) {
        int tmp = array[i];
        array[i] = array[j];
        array[j] = tmp;
    }
    private static int parttion(int[] array,int left,int right) {
        int i = left;//记录这个位置
        int tmp = array[left];
        while (left < right) {
            while (left < right && array[right] >= tmp) {
                right--;
            }
            while (left < right && array[left] <= tmp) {
                left++;
            }
            swap(array,left,right);
        }
        swap(array,left,i);
        return left;
    }

    private static void quick(int[] array,int start,int end) {
        if(start >= end) {
            return;
        }
        int pivot = parttion(array,start,end);

        quick(array,start,pivot-1);//左树

        quick(array,pivot+1,end);//右树
    }

    public static void quickSort(int[] array) {
        quick(array,0,array.length-1);
    }

    public static void main1(String[] args) {
        int[] array = {1,9,5,8,6,3,5,7};

        int[] array1 = new int[10];
        quickSort(array);
        System.out.println(Arrays.toString(array));
    }
}
